#include "StdAfx.h"
#include "TableTrain.h"


CTableTrain::CTableTrain()
{
    m_pConnection = NULL;
    m_strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=pis.mdb;Jet OLEDB:Database Password='admin'";
}


CTableTrain::~CTableTrain(void)
{
    Close();
}

BOOL CTableTrain::Open()
{
    if(m_pConnection)
    {
        return TRUE;
    }

    HRESULT hr;
    hr = m_pConnection.CreateInstance(__uuidof(Connection));
    if(FAILED(hr))
    {
        m_pConnection = NULL;
        return FALSE;
    }
    hr = m_pConnection->Open(m_strConn, "", "", adModeUnknown);
    if(FAILED(hr))
    {
        m_pConnection.Release();
        m_pConnection = NULL;
        return FALSE;
    }
    return TRUE;
}

void CTableTrain::Close()
{
    if(!m_pConnection)
    {
        m_pConnection->Close();
        m_pConnection = NULL;
    }
}

void CTableTrain::GetTrains(vector<TrainInfo> &vecTrains)
{
    if(!Open())
    {
        return;
    }
    HRESULT hr;
    _RecordsetPtr pRecordset = NULL; 
    hr = pRecordset.CreateInstance(__uuidof(Recordset));
    if(FAILED(hr))
    {
        return;
    }
    _bstr_t strSql = "SELECT TrainID, TrainName, TrainIp1, TrainIp2, GBId FROM Train";
    hr = pRecordset->Open(strSql, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic,adCmdText);
    if(FAILED(hr))
    {
        pRecordset.Release();
        pRecordset = NULL;
        return;
    }
    int iRow=0;
    while(!pRecordset->adoEOF)
    {
        TrainInfo train;
        _bstr_t str=(_bstr_t) pRecordset->GetCollect("TrainID");
        train.TrainID = atoi(str);

        str=(_bstr_t) pRecordset->GetCollect("TrainName");
        strcpy(train.TrainName, str);

		str=(_bstr_t) pRecordset->GetCollect("TrainIp1");
		strcpy(train.TrainIp1 , str);

		str=(_bstr_t) pRecordset->GetCollect("TrainIp2");
		strcpy(train.TrainIp2 , str);

		str=(_bstr_t) pRecordset->GetCollect("GBId");
		strcpy(train.TrainGBId, str);

        pRecordset->MoveNext();
        vecTrains.push_back(train);
        iRow++;
    }
    pRecordset.Release();
    pRecordset = NULL;
}


void CTableTrain::GetDevices(int TrainID, vector<ChannelInfo> &vecDevice)
{
    if(!Open())
    {
        return;
    }

    HRESULT hr;
    _RecordsetPtr pRecordset = NULL; 
    hr = pRecordset.CreateInstance(__uuidof(Recordset));
    if(FAILED(hr))
    {
        return;
    }
	char Sql[512] = {0};
    sprintf(Sql, "SELECT * FROM [Train], [Cam] WHERE [Train].TrainID=[Cam].TrainID and [Cam].TrainID = %d", TrainID);
    _bstr_t strSql = Sql;
    hr = pRecordset->Open(strSql, m_pConnection.GetInterfacePtr(), adOpenDynamic, adLockOptimistic,adCmdText);
    if(FAILED(hr))
    {
        pRecordset.Release();
        pRecordset = NULL;
        return;
    }
    int iRow=0;
    while(!pRecordset->adoEOF)
    {
        ChannelInfo ChInfo;
        _bstr_t str=(_bstr_t) pRecordset->GetCollect("Cam.TrainID");
        ChInfo.TrainID = atoi(str);

        str=(_bstr_t) pRecordset->GetCollect("CamID");
        ChInfo.ChannelID = atoi(str);

        str=(_bstr_t) pRecordset->GetCollect("CamName");
        strcpy(ChInfo.ChannelName, str);

		str=(_bstr_t) pRecordset->GetCollect("Cam.GBId");
		strcpy(ChInfo.ChannelGBId, str);

		str=(_bstr_t) pRecordset->GetCollect("Train.GBId");
		strcpy(ChInfo.TrainGbId, str);

        pRecordset->MoveNext();
        vecDevice.push_back(ChInfo);
        iRow++;
    }
    pRecordset.Release();
    pRecordset = NULL;
}
